<?php

namespace app\admin\model;

use app\common\model\Category;
use think\Model;
use think\model\relation\BelongsTo;


class Article extends Model
{

    

    

    // 表名
    protected $name = 'article';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'is_home_text',
        'status_text'
    ];
    
    public function getImageAttr($value)
    {
        return config("upload.cdnurl") . $value."?x-oss-process=image/resize,w_200,h_200";
    }

    /**
     * 关联分类
     * @return BelongsTo
     */
    public function inCategory(): BelongsTo
    {
        return $this->belongsTo(Category::class,'category_id','id')->bind([
            'category_name' => 'name'
        ]);
    }

    protected static function init()
    {
        self::afterInsert(function ($row) {
            if (!$row['weigh']) {
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            }
        });
    }

    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2'),'3' => __('Type 3')];
    }

    public function getIsHomeList()
    {
        return ['1' => __('Is_home 1'), '2' => __('Is_home 2')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsHomeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_home']) ? $data['is_home'] : '');
        $list = $this->getIsHomeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




}
